import { login, logout, getInfo } from '@/api/auth/user'
import { getToken, setToken, removeToken, setUser, removeUser } from '@/utils/auth'
import { resetRouter } from '@/router'

import md5 from 'js-md5'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: ''
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_USER: (state, userJson) => {
    state.user = userJson
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const data = new FormData()
    data.append('username', userInfo.username)
    data.append('password', md5(userInfo.password))
    return new Promise((resolve, reject) => {
      login(data).then(response => {
        const data = response.data
        //判断权限必须是管理员才能登陆此平台
        var flag = false
        for (var i = 0; i < data.accessList.length; i++) {
          if (data.accessList[i] == 'superManager'
            || data.accessList[i] == 'loginAccess') {
            flag = true
            break
          }
        }
        if (flag) {
          commit('SET_TOKEN', data.token)
          commit('SET_USER', JSON.stringify(data))
          setToken(data.token)
          setUser(JSON.stringify(data))
          commit('SET_NAME', data.name)
          resolve()

        } else {
          reject('您没有权限 登陆平台')
        }
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      const data = new FormData()
      data.append('token', getToken())
      getInfo(data).then(response => {
        const data = response.data
        if (!data || response.code !== 200) {
          reject('认证失败，请重新登录')
        }

        commit('SET_TOKEN', data.token)
        commit('SET_USER', JSON.stringify(data))
        setUser(JSON.stringify(data))
        commit('SET_NAME', data.name)

        // commit('SET_AVATAR', avatar)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      const data = new FormData()
      data.append('token', state.token)

      logout(data).then(() => {
        removeToken() // must remove  token  first
        removeUser()
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  //重新跳转到登录页面
  reLogin({ commit, state }) {
    return new Promise((resolve, reject) => {
      removeToken() // must remove  token  first
      removeUser()
      resetRouter()
      commit('RESET_STATE')
      resolve()
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

